<?php
/**
 * @package tests
 */
require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../../../plugins/GraduaUtil/lib/test/Gradua_PHPUnit_Framework_TestCase.php';
/**
 * @package tests
 */

/**
 * There are also lots of other tests for the JsonSchema on the Selenium.
 *
 */
class SchemaValidatorTest extends Gradua_PHPUnit_Framework_TestCase
{
    public function setup()
    {
      parent::setUp();
      ServiceValidator::$validateOutput = true;
      ServiceValidator::$validateInput  = true;
      JsonSchema::$checkMode = JsonSchema::CHECK_MODE_TYPE_CAST;
    }
    /**
     * {@source}
     */
    public function testShouldWorkWithMultipleStaticCallsToValidate() {
      #first a validation with error and then one without to see if errors
      #get cleared.
      $schema = json_decode('{"type":"integer"}');
      $json = json_decode('"a"');
      $result = JsonSchema::validate($json,$schema);
      $this->assertFalse($result->valid);
      $schema = json_decode('{"type":"integer"}');
      $json = json_decode('5');
      $result = JsonSchema::validate($json,$schema);
      $this->assertTrue($result->valid);
    }

    public function testShouldAcceptArrayAsObjectOnCastTypeMode() {

      $schema = json_decode('{
        "type":"object",
        "properties":{
          "a":{"type":"integer","maximum":8.0}
        }
      }');

      $json = array(
        "a"=>9
      );

      $result = JsonSchema::validate($json,$schema);
      $this->assertTrue($result->valid);

      $schema = json_decode(
'{
    "type": "object",
    "properties": {
        "a": {
            "type": "integer",
            "maximum": 8
        },
        "b": {
            "type": "object",
            "properties": {
                "p1": {
                    "type": "integer"
                },
                "p2": {
                    "type": "string"
                }
            }
        }
    }
}');

      $json = array(
        "a"=>9
      );

      $result = JsonSchema::validate($json,$schema);
      $this->assertEquals(count($result->errors), 5);

      $json = array(
        "a"=>8,
        "b"=>array(
          "p1"=>5,
          "p2"=>"a"
        )
      );

      $result = JsonSchema::validate($json,$schema);
      $this->assertTrue($result->valid);

      $json = array(
        "a"=>8,
        "b"=>array(
          "p1"=>"d",
          "p2"=>"a"
        )
      );

      $result = JsonSchema::validate($json,$schema);
      $this->assertFalse($result->valid);
    }


    /**
    * coisa mais esquisita    * generated 2009-04-24 18:29    */
    function testCoisaMaisEsquisita() {
      $this->markTestIncomplete('Not yet implemented.');
    }

}